<template>
  <div class="goods">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div slot="header">
        <el-input
          placeholder="请输入内容"
          clearable
          v-model="queryGoodsInfo.query"
          @clear="getGoodsList"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getGoodsList"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
      </div>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="500px"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
        ></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="160px">
          <template #default="{ row }">{{
            row.add_time | dateFormat
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template #default="{ row }">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="delGood(row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryGoodsInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryGoodsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsListApi, delGoodsApi } from "@/api/user";
export default {
  name: "GoodsIndex",
  data() {
    return {
      queryGoodsInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [],
      total: 0,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const res = await getGoodsListApi(this.queryGoodsInfo);
      // console.log(res.data);
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户列表失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.queryGoodsInfo.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newPage) {
      this.queryGoodsInfo.pagenum = newPage;
      this.getGoodsList();
    },
    async delGood(id) {
      try {
        await this.$confirm("确认删除商品！");
        const res = await delGoodsApi(id);
        // console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("商品删除失败！");
        }
        this.$message.success("商品删除成功！");
        this.getGoodsList();
      } catch (error) {
        this.$message.info("已取消删除！");
      }
    },
    goAddGoodsPage() {
      this.$router.push("/add");
    },
  },
};
</script>

<style lang="scss" scoped>
.goods {
  .el-breadcrumb {
    margin-bottom: 15px;
  }
  .el-card {
    .el-input {
      width: 33%;
      margin-right: 20px;
    }
    .el-table {
      margin-bottom: 15px;
    }
  }
}
</style>
